﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace GrayParrot.Web.Formly
{
    [Serializable]
    public class FieldItem 
    {
        [JsonProperty(PropertyName="key")]
        public string Key { get; set; }

        [JsonProperty(PropertyName = "type", NullValueHandling = NullValueHandling.Ignore)]
        public FormFieldTypes? Type { get; set; }
        
        [JsonProperty(PropertyName = "defaultValue")]
        public object DefaultValue { get; set; }
        
        [JsonProperty(PropertyName = "templateOptions")]
        public TemplateOptions TemplateOptions { get; set; }

        [JsonProperty(PropertyName = "expressionProperties", DefaultValueHandling = DefaultValueHandling.Ignore)]
        public ExpressionProperties ExpressionProperties { get; set; }

        [JsonProperty(PropertyName = "hideExpression", DefaultValueHandling = DefaultValueHandling.Ignore)]
        public object HideExpression { get; set; }

        [JsonProperty(PropertyName = "className", DefaultValueHandling = DefaultValueHandling.Ignore)]
        public object ClassName { get; set; }

        [JsonProperty(PropertyName = "fieldGroup", DefaultValueHandling = DefaultValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore)]
        public List<FieldItem> Childs { get; set; }

        [JsonIgnore()]
        public string FieldGroupName { get; set; }
        [JsonIgnore()]
        public int PositionInRow { get; set; }
        [JsonIgnore()]
        public int Row { get; set; }

        public FieldItem()
        {
            this.Childs = null;
            this.Type = null;
        }

        public bool ShouldSerializeChilds()
        {
            bool retValue = false;

            if (this.Childs != null)
            {
                retValue = this.Childs.Count() > 0;
            }
            else 
            {
                retValue = false;
            }

            return retValue;
        }
    }
}
